package controller;

import po.Course;
import po.Grade;
import service.CourseService;
import service.GradeService;
import service.serviceImpl.CourseServiceImpl;
import service.serviceImpl.GradeServiceImpl;
import util.ServletRepeat;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author 润
 */
@WebServlet(name = "CourseServlet",urlPatterns = "/CourseServlet")
public class CourseServlet  extends BaseServlet {

    private CourseService service = new CourseServiceImpl();
    private GradeService gradeService = new GradeServiceImpl();

    public void queryAllCourse(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String g = request.getParameter("gid");
        int gradeId = Integer.parseInt(g);
        Grade grade = gradeService.queryOne(gradeId);
        Integer collegeId = grade.getCid();
        List<Course> courses = service.queryAll(collegeId, gradeId);
        if (courses.size() > 0) {
            request.setAttribute("courses", courses);
            request.getRequestDispatcher("/allCourse.jsp").forward(request, response);
        } else {
            request.setAttribute("img", "暂时无课程！");
            request.getRequestDispatcher("/emptyCourse.jsp").forward(request, response);
        }
    }

    public void addCourse(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String name = request.getParameter("course");
        String place = request.getParameter("place");
        String teacher = request.getParameter("teacher");
        String time = request.getParameter("time");
        String tatal = request.getParameter("total");
        String beginTime = request.getParameter("beginTime");
        int total = Integer.parseInt(tatal);
        String g = request.getParameter("gid");
        int gradeId = Integer.parseInt(g);
        Grade grad = gradeService.queryOne(gradeId);
        Integer collegeId = grad.getCid();
        Course course = new Course();
        course.setNumber(0);
        course.setTotal(total);
        course.setTIME(time);
        course.setTeacher(teacher);
        course.setPlace(place);
        course.setBeginTime(beginTime);
        course.setName(name);
        course.setCollegeId(collegeId);
        course.setGradeId(gradeId);
        service.insert(course);
        boolean result1 = true;
        String json1 = ServletRepeat.getJson(result1);
        response.getWriter().write(json1);
    }

    public void queryOneCourse(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String i = request.getParameter("id");
        int id = Integer.parseInt(i);
        Course course = service.queryOne(id);
        request.setAttribute("course",course);
        request.getRequestDispatcher("/updateCourse.jsp").forward(request, response);
    }


    public void updateCourse(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String i = request.getParameter("id");
        int id = Integer.parseInt(i);
        String name = request.getParameter("course");
        String place = request.getParameter("place");
        String teacher = request.getParameter("teacher");
        String time = request.getParameter("time");
        String tatal = request.getParameter("total");
        String beginTime = request.getParameter("beginTime");
        int total = Integer.parseInt(tatal);
        String g = request.getParameter("gid");
        int gradeId = Integer.parseInt(g);
        Grade grad = gradeService.queryOne(gradeId);
        Integer collegeId = grad.getCid();
        Course course = new Course();
        course.setNumber(0);
        course.setTotal(total);
        course.setTIME(time);
        course.setTeacher(teacher);
        course.setPlace(place);
        course.setBeginTime(beginTime);
        course.setName(name);
        course.setCollegeId(collegeId);
        course.setGradeId(gradeId);
        course.setId(id);
        service.update(course);
        boolean result1 = true;
        String json1 = ServletRepeat.getJson(result1);
        response.getWriter().write(json1);
    }
    public void deleteCourse(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String d = request.getParameter("id");
        int id = Integer.parseInt(d);
        Course course = new Course();
        course.setId(id);
        Integer gradeId = course.getGradeId();
        service.delete(course);
        Grade grade = gradeService.queryOne(gradeId);
        Integer collegeId = grade.getCid();
        List<Course> courses = service.queryAll(collegeId, gradeId);
        if (courses.size() > 0) {
            request.setAttribute("courses", courses);
            request.getRequestDispatcher("/allCourse.jsp").forward(request, response);
        } else {
            request.setAttribute("img", "暂时无课程！");
            request.getRequestDispatcher("/emptyCourse.jsp").forward(request, response);
        }

    }
}
